package com.doitedu.hbase.client;

import com.doitedu.hbase.utils.HbaseUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor;
import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder;
import org.apache.hadoop.hbase.client.Connection;

import java.io.IOException;

/**
 * @Date 2021/12/13
 * @Created by HANGGE
 * @Description 修改表结构
 */
public class C09_AlterTable {
    public static void main(String[] args) throws Exception {
        Connection conn = HbaseUtil.getConnection();
        Admin admin = HbaseUtil.getAdmin(conn);
        TableName city = TableName.valueOf("tb_city");
        ColumnFamilyDescriptorBuilder info2b = ColumnFamilyDescriptorBuilder.newBuilder("info2".getBytes());
        // 设置列族的属性
        info2b.setMaxVersions(3);
        // 修改已存在列族的属性值   列族不存在报错
        ColumnFamilyDescriptor columnFamilyDescriptor = info2b.build();
        admin.modifyColumnFamily(city ,columnFamilyDescriptor);

        admin.close();
        conn.close();

    }

    private static void deleteCf(Admin admin, TableName city) throws IOException {
        /**
         * 参数一  表
         * 参数二  列族名
         */
        admin.deleteColumnFamily(city, "info2".getBytes());
    }

    private static void addCf(Admin admin, TableName city) throws IOException {
        /**
         * 参数一  表
         * 参数二  列族描述器
         */
        ColumnFamilyDescriptorBuilder info2b = ColumnFamilyDescriptorBuilder.newBuilder("info2".getBytes());
        // 设置列族的属性
        info2b.setMaxVersions(3);
        ColumnFamilyDescriptor columnFamilyDescriptor = info2b.build();
        admin.addColumnFamily(city , columnFamilyDescriptor);
    }
}
